package com.elasticjob.config;

import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RegistryCenterConfig {

    @Bean
    private static CoordinatorRegistryCenter createRegistryCenter(
            @Value("${zookeeper.host}") String host,
            @Value("${zookeeper.namespace}") String namespace
    )
    {
        //配置zk地址,调度任务的组名
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(
                host,//第一个参数:zookeeper地址
                namespace);//第二个参数:namespace 命名空间,zookeeper会开辟一个内存放我们任务信息
        zookeeperConfiguration.setSessionTimeoutMilliseconds(100);
        CoordinatorRegistryCenter regCenter =
                new ZookeeperRegistryCenter(zookeeperConfiguration);
        regCenter.init();
        return regCenter;
    }
}